#include "global.h"
#include "data.h"

flowshop(deque <Activity> acts, deque <int> sched, int nbStages){
	

	deque <int> av;
	int i, j, stage,  k ;

	for ( i = 0; i <  nbStages ;i ++)
		 av.push_back(0);

	for ( j = 0; j < sched.size(); j++) {
		for (stage = 0; stage < nbStages; stage++) {
			
			if ( stage== 0) 
				acts[sched[j] * nbStages + stage].
				setRtime(sched[j], stage, 0);
			else
				acts[sched[j] * nbStages + stage].
				setRtime(sched[j], stage, 
				acts[sched[j] * nbStages
				+ stage-1].getEtime(sched[j],stage-1));

			if ( j == 0 ) 
				av[j] = 0;
			else
				av[stage] = acts[sched[j - 1 ] * nbStages 
					+ stage].getEtime( sched[j - 1], stage);

			int kkk = max(acts[sched[j]*nbStages + stage].
				getRtime( sched[j], stage),av[stage] );

			//define starting time

			acts[sched[j] * nbStages + stage].
				setEStime( sched[j], stage, 
				max(acts[i * nbStages
				+ stage].getRtime(sched[j], stage), av[stage]));
			
			acts[sched[j] * nbStages + stage].
				setStime ( sched[j], 
				stage, kkk);		

			/*
			printf("%d%s %d%s %d%s %d%s %d%s %d%s %d\n", 
				sched[j],",",
				stage,",",
				acts[sched[j]*nbStages +stage].
				getRtime( sched[j], stage), ",", 		
				av[stage], ",",
				acts[sched[j]*nbStages +stage].
				getStime( sched[j], stage), ",", 
				acts[sched[j]*nbStages + stage].
				getPtime( sched[j], stage),  ",",
				acts[sched[j]*nbStages + stage].
				getEtime( sched[j], stage) );
			*/
		}
		
	//	cout << endl;
	}

	int makespan = 0;
	for ( k = 0; k < sched.size(); k ++ ) {		
		for ( j = 0; j < nbStages; j ++ ) {
			if ( makespan < 
				acts[sched[k] * nbStages + 
				j].getEtime(sched[k],j))
				makespan = acts[sched[k] * 
				nbStages + j].				
				getEtime(sched[k],j) ;
		}		

	}
	return makespan;
}



